package com.opencloud.ywt.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.iot.client.model.entity.IotNStreng;
import com.opencloud.ywt.client.model.entity.*;
import com.opencloud.ywt.provider.mapper.*;
import com.opencloud.ywt.provider.service.BhyfStudentService;
import io.swagger.models.auth.In;
import lombok.Data;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 动量服务
 * @author yeyong
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class BhyfStudentServiceImpl implements BhyfStudentService {
    @Autowired
    private BhyfStudentMapper studentMapper;

    @Autowired
    private BhyfBflAngleMapper bhyfBflAngleMapper;

    @Autowired
    private BhyfXhbSleepClassMapper bhyfXhbSleepClassMapper;

    @Autowired
    private BhyfXhbSleepPersonMapper bhyfXhbSleepPersonMapper;

    @Autowired
    private BhyfXhbWatchMapper bhyfXhbWatchMapper;

    @Autowired
    private BhyfBflAngleBeaconsMapper bhyfBflAngleBeaconsMapper;

    @Autowired
    private BhyfBflCourseMapper bhyfBflCourseMapper;

    @Override
    public List<BhyfStudent> getBhyfStudentList(Integer gid,Integer gradenum, Integer classid,String studentname,Integer personid) {
        return studentMapper.getBhyfStudentList(gid,gradenum,classid,studentname,personid);
    }

    @Override
    public List<BhyfBflAngle> getBhyfClassAngleList(Integer gid, Integer classid)
    {
        return bhyfBflAngleMapper.getClassAngleList(gid,classid);
    }

    @Override
    public List<BhyfBflAngleBeacon> getBhyfClassAngleBeaconList(Integer angleid)
    {
        return bhyfBflAngleMapper.getAngleBeaconList(angleid);
    }

    @Override
    public BhyfBflAngle addBhyfBflAngle(BhyfBflAngle entiy)
    {
        bhyfBflAngleMapper.insert(entiy);
        return  entiy;
    }

    @Override
    public BhyfBflAngle getBhyfBflAngleById(Integer id)
    {
        BhyfBflAngle entiy = bhyfBflAngleMapper.selectById(id);
        return  entiy;
    }

    @Override
    public Integer updateBhyfBflAngle(BhyfBflAngle entiy)
    {
        return bhyfBflAngleMapper.updateById(entiy);
    }

    @Override
    public List<BhyfBflAngle> getAngleByBeacon(String beacon)
    {
        return bhyfBflAngleMapper.getAngleByBeacon(beacon);
    }

    @Override
    public IPage<BhyfXhbSleepClass> findBhyfXhbSleepClassListPage(PageParams pageParams, String startTime, String endTime) {
        BhyfXhbSleepClass query = pageParams.mapToObject(BhyfXhbSleepClass.class);
        QueryWrapper<BhyfXhbSleepClass> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGardenid()), BhyfXhbSleepClass::getGardenid,query.getGardenid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGradenum()), BhyfXhbSleepClass::getGradenum,query.getGradenum());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getClassid()), BhyfXhbSleepClass::getClassid,query.getClassid());
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbSleepClass::getSleepdate, startTime, endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbSleepClass::getClassid);
        return bhyfXhbSleepClassMapper.selectPage(new Page(pageParams.getPage(), pageParams.getLimit()), queryWrapper);
    }


    @Override
    public List<BhyfXhbSleepPerson> findBhyfXhbSleepPersonListPage(int personid, String startTime, String endTime) {
        List<BhyfXhbSleepPerson> returnList = new ArrayList<>();
        QueryWrapper<BhyfXhbSleepPerson> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(personid), BhyfXhbSleepPerson::getPersonid,personid);
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),BhyfXhbSleepPerson::getSleepDate, startTime, endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbSleepPerson::getSleepDate);
        returnList = bhyfXhbSleepPersonMapper.selectList(queryWrapper);
        return returnList;
    }

    @Override
    public List<BhyfXhbWatch> findBhyfXhbWatchList(int personid,int status,int watchtype){
        QueryWrapper<BhyfXhbWatch> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(personid), BhyfXhbWatch::getPersonid,personid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(watchtype), BhyfXhbWatch::getWatchtype,watchtype);
        queryWrapper.lambda().eq(status>0, BhyfXhbWatch::getWatchstatus,status);
        queryWrapper.lambda().orderByDesc(BhyfXhbWatch::getId);
        return bhyfXhbWatchMapper.selectList(queryWrapper);
    }

    @Override
    public Integer addBhyfBflAngleBeacon(BhyfBflAngleBeacon entiy)
    {
        return bhyfBflAngleBeaconsMapper.insert(entiy);
    }


    @Override
    public List<BhyfBflCourse> findBhyfCourseList(int gid){
        QueryWrapper<BhyfBflCourse> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfBflCourse::getGardenid,gid);
        queryWrapper.lambda().orderByDesc(BhyfBflCourse::getId);
        return bhyfBflCourseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BhyfRecordRemark> getBhyfRecordRemarkList(int personid,String startdate,String enddate)
    {
        return studentMapper.getBhyfRecordRemarkList(personid,startdate,enddate);
    }


    @Override
    public List<BhyfTempCheck> getBhyfTempCheckList(int personid,String startdate,String enddate)
    {
        return studentMapper.getBhyfTempCheckList(personid,startdate,enddate);
    }

    @Override
    public List<BhyfRecordRemark> getBhyfRecordRemarkListByClass(int classid,String checkdate)
    {
        return studentMapper.getBhyfRecordRemarkListByClass(classid,checkdate);
    }

    @Override
    public List<BhyfTempCheck> getBhyfTempCheckListByClass(int classid,String checkdate)
    {
        return studentMapper.getBhyfTempCheckListByClass(classid,checkdate);
    }

    @Override
    public List<BhyfCardRecordDateResult> getGardenCardRecordGroupByDate(int gid,String startdate,String enddate)
    {
        return studentMapper.getGardenCardRecordGroupByDate(gid,startdate,enddate);
    }

    @Override
    public List<BhyfCardRecordDateResult> getClassCardRecordGroupByDate(int classid,String startdate,String enddate)
    {
        return studentMapper.getClassCardRecordGroupByDate(classid,startdate,enddate);
    }

    @Override
    public List<BhyfCardRecordClassResult> getGardenCardRecordGroupByClass(int gid,String startdate,String enddate)
    {
        return studentMapper.getGardenCardRecordGroupByClass(gid,startdate,enddate);
    }

    @Override
    public List<BhyfGardenData> getGardenDataGroupByClass(Integer gid,String checkdate){
        return studentMapper.getGardenDataGroupByClass(gid,checkdate);
    }

    @Override
    public List<BhyfClassData> getClassDataListByDate(Integer gid,Integer classid,String checkdate){
        return studentMapper.getClassDataList(gid,classid,checkdate);
    }

    @Override
    public List<BhyfGardenInfo> getGardenInfoList(String keyword,Integer provinceid,Integer gid){
        return studentMapper.getGardenInfoList(keyword,provinceid,gid);
    }


    @Override
    public Integer deleteBhyfBflAngleBeacon(BhyfBflAngleBeacon entiy)
    {
        QueryWrapper<BhyfBflAngleBeacon> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(entiy), BhyfBflAngleBeacon::getAngleid,entiy.getAngleid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(entiy), BhyfBflAngleBeacon::getBeaconaddress,entiy.getBeaconaddress());
        return bhyfBflAngleBeaconsMapper.delete(queryWrapper);
    }


    @Override
    public List<BhyfBflAngleBeacon> isExistBeacon(Integer angleid,String beacon)
    {
        QueryWrapper<BhyfBflAngleBeacon> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(angleid), BhyfBflAngleBeacon::getAngleid,angleid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(beacon), BhyfBflAngleBeacon::getBeaconaddress,beacon);
        return bhyfBflAngleBeaconsMapper.selectList(queryWrapper);
    }

    @Override
    public List<BhyfStudent> getAllBhyfStudentList(Integer gid, Integer classid,String studentname,Integer personid) {
        return studentMapper.getAllBhyfStudentList(gid,classid,studentname,personid);
    }

    @Override
    public List<BhyfBdbWatchBattery> getWatchBatteryList(Integer gid, Integer classid,Integer personid,String starttime,String endtime)
    {
        return studentMapper.getWatchBatteryList(gid,classid,personid,starttime,endtime);
    }

    @Override
    public List<BhyfBdbBeaconBattery> getBeaconBatteryList(Integer gid, Integer classid,String starttime,String endtime)
    {
        return studentMapper.getBeaconBatteryList(gid,classid,starttime,endtime);
    }

    @Override
    public List<BhyfBdbWatchVersion> getWatchVersionList(Integer gid, Integer classid,Integer personid,String starttime,String endtime)
    {
        return studentMapper.getWatchVersionList(gid,classid,personid,starttime,endtime);
    }

    @Override
    public List<BhyfBdbBeaconBattery> getBeaconBatteryLastList(Integer gid,String starttime,String endtime)
    {
        return studentMapper.getBeaconBatteryLastList(gid,starttime,endtime);
    }

    @Override
    public List<BhyfBdbWatchBattery> getWatchBatteryLastList(String mac,String starttime,String endtime)
    {
        return studentMapper.getWatchBatteryLastList(mac,starttime,endtime);
    }

    @Override
    public List<BhyfBdbWatchBattery> getGardenBadBatteryWatchList(Integer gid,String starttime, String endtime)
    {
        return studentMapper.getGardenBadBatteryWatchList(gid,starttime,endtime);
    }
}